﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class API_A_Userinfomanage : System.Web.UI.Page
{
    /// <summary>
    /// USERSYSTEM INFORMATION.API 用户系统API
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        string returnStr = string.Empty;
        string editType = Request.Form["edit"];//操作类型
        switch (editType)
        {
            case "set":
                {
                    returnStr = InformationEdit();
                    break;
                }
            case "list":
                {
                    returnStr = InformationList();
                    break;
                }
            case "out":
                {
                    returnStr = UserOut();
                    break;
                }
            case "uppwd":
                {
                    returnStr = UpPwd();
                    break;
                }
            default: break;
        }
        Response.Write(returnStr);
    }
    /// <summary>
    /// 修改用户信息
    /// </summary>
    /// <returns></returns>
    protected string InformationEdit()
    {
        BLL.UserInformation bll = new BLL.UserInformation();
        string returnStr = string.Empty;
        bool flag = bll.CheckUserInformation(Session["userid"].ToString());
        if (flag)
        {
            returnStr = InformationUpdate();
        }
        else
        {
            returnStr = InformationAdd();
        }
        return returnStr;
    }
    /// <summary>
    /// 添加用户信息
    /// </summary>
    /// <returns></returns>
    protected string InformationAdd()
    {
        MODEL.UserInformation model = new MODEL.UserInformation();
        BLL.UserInformation bll = new BLL.UserInformation();
        model.UID = Session["userid"].ToString();
        model.Ucity = Request.Form["c"];
        model.Uemail = Request.Form["e"];
        model.UnickName = Request.Form["n"];
        model.Uprovince = Request.Form["p"];
        model.Uqq = Request.Form["q"];
        model.UregTime = DateTime.Now;
        model.Usex = Convert.ToBoolean(Convert.ToInt32(Request.Form["s"]));
        model.Utel = Request.Form["t"];

        return bll.UserInformationAdd(model).ToString().ToLower();
    }
    /// <summary>
    /// 用户信息列表
    /// </summary>
    /// <returns></returns>
    protected string InformationList()
    {
        MODEL.UserInformation model = new MODEL.UserInformation();
        BLL.UserInformation bll = new BLL.UserInformation();
        string returnStr = string.Empty;
        model = bll.GetUserInformationModel(Session["userid"].ToString());
        returnStr = "{\"n\":\"" + model.UnickName + "\",\"s\":\"" + Convert.ToInt32(model.Usex) + "\",\"q\":\"" + model.Uqq + "\",\"e\":\"" + model.Uemail + "\",\"t\":\"" + model.Utel + "\",\"p\":\"" + model.Uprovince + "\",\"c\":\"" + model.Ucity + "\",\"r\":\"" + model.UregTime + "\"}";
        return returnStr;
    }
    /// <summary>
    /// 用户注销
    /// </summary>
    /// <returns></returns>
    protected string UserOut()
    {
        Session["username"] = string.Empty;
        Session["userid"] = string.Empty;
        return "true";
    }
    /// <summary>
    /// 修改用户信息
    /// </summary>
    /// <returns></returns>
    protected string InformationUpdate()
    {
        MODEL.UserInformation model = new MODEL.UserInformation();
        BLL.UserInformation bll = new BLL.UserInformation();
        model.UID = Session["userid"].ToString();
        model.Ucity = Request.Form["c"];
        model.Uemail = Request.Form["e"];
        model.UnickName = Request.Form["n"];
        model.Uprovince = Request.Form["p"];
        model.Uqq = Request.Form["q"];
        model.Usex = Convert.ToBoolean(Convert.ToInt32(Request.Form["s"]));
        model.Utel = Request.Form["t"];

        return bll.UserInformationUpdate(model).ToString().ToLower();
    }
    /// <summary>
    /// 修改密码
    /// </summary>
    /// <returns></returns>
    protected string UpPwd()
    {
        string returnStr = string.Empty;
        MODEL.UserLogon model = new MODEL.UserLogon();
        BLL.UserLogon bll = new BLL.UserLogon();
        model.UID = Session["userid"].ToString();
        model.UlogonName = Session["username"].ToString();
        model.UpassWord = EncrypterMD5.MD5.Md5(Request.Form["old"].ToString(), 32);
        bool flag = bll.CheckPassword(model);
        if (flag)
        {
            model.UpassWord = EncrypterMD5.MD5.Md5(Request.Form["ok"],32);
            flag = bll.UserLogonUpdatePassword(model);
        }
        else
        {
            flag = false;
        }
        returnStr = "{\"err\":\"" + Convert.ToInt32(flag) + "\"}";
        return returnStr;
    }
}
